package com.yang.train.business.controller.web;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.yang.train.business.req.ConfirmOrderBuyTicketReq;
import com.yang.train.business.service.ConfirmOrderService;
import com.yang.train.common.exception.BusinessExceptionEnum;
import com.yang.train.common.resp.CommonResp;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/confirm-order")
public class ConfirmOrderController {

    @Resource
    private ConfirmOrderService confirmOrderService;

    private static final Logger LOG = LoggerFactory.getLogger(ConfirmOrderService.class);

    /**
     * 购票业务
     */
    @PostMapping("/do")
    @SentinelResource(value = "confirmOrderDo",blockHandler = "doConfirmBlock")
    public CommonResp<Object> doConfirm(@Valid @RequestBody ConfirmOrderBuyTicketReq req) {
        confirmOrderService.doConfirm(req);
        return new CommonResp<>();
    }
    public  CommonResp<Object> doConfirmBlock(ConfirmOrderBuyTicketReq req, BlockException e){
        LOG.info("购票被限流：{}",req);
        CommonResp<Object> commonResp = new CommonResp<>();
        commonResp.setSuccess(false);
        commonResp.setMessage(BusinessExceptionEnum.CONFIRM_ORDER_FLOW_EXCEPTION.getDesc());
        return commonResp;
    }

}
